<?
if (!isset($r)) $r = "../";

require_once($r."_engine/header_nocache.php");
require_once($r."_engine/c_account.php");
require_once($r."_engine/c_plan.php");
require_once($r."_config/ipn.php");

$account = GetActiveAccount();
$plans = CPlan::GetPlans();





//if user logged in
if ($_POST || $g_session->IsActive()) {
	$form_paypal = "	
	<form method='post'  name='paymentForm' id='paymentForm' action='https://www.paypal.com/cgi-bin/webscr' > 
 
	<input type='hidden' name='cmd' value='_xclick' /> 
	<input type='hidden' name='business' value='$ipn_paypal_business' /> 
	<input type='hidden' name='item_name' value='[%TITLE%]' />
	<input type='hidden' name='item_number' value='[%ID%]' />
	<input type='hidden' name='return' value='$ipn_paypal_return' /> 
	<input type='hidden' name='notify_url' value='$ipn_paypal_notify_url' /> 
	<input type='hidden' name='rm' value='2' /> 
	<input type='hidden' name='cancel_return' value='$ipn_paypal_cancel_return' /> 
	<input type='hidden' name='no_shipping' value='1' /> 
	<input type='hidden' name='no_note' value='1' /> 
	<input type='hidden' name='invoice' value='[%INVOICE%]' /> 
	<input type='hidden' name='amount' value='[%COST%]' /> 
	<input type='hidden' name='currency_code' value='[%CURRENCY%]' />

	</form>";

	$a = array(
		'INVOICE'		=> CPlan::GetInvoice($_POST['plan'], $_POST['account']),
		'ID'	      	=> $_POST['plan'],
		'TITLE'      	=> $_POST['description'].' - '.$_POST['title'],
		'COST'     		=> $_POST['cost'],
		'CURRENCY'     	=> $_POST['currency']
	);

	echo process_template($form_paypal, $a);
	
	echo "<h2>Forwarding to PayPal...</h2>";
	echo "<script> document.getElementById('paymentForm').submit(); </script>";
} //else exit ("error ");





function process_template($template, $data)
{
    foreach ($data as $k => $v)
    {
        $template = str_replace("[%".$k."%]", $v, $template);
    }
    return $template;
}



?>